package com.wyh.service.serviceImpl.feemanagementserviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.domain.dto.feemanagementdto.*;
import com.wyh.domain.po.feemanagementpo.PaymentRecords;
import com.wyh.domain.po.feemanagementpo.PaymentRecordsInfo;
import com.wyh.domain.vo.feemanagementvo.AllPaymentRecordsVo;
import com.wyh.domain.vo.feemanagementvo.PaymentRecordsVos;
import com.wyh.mapper.feemanagementmapper.PaymentRecordsMapper;
import com.wyh.service.feemanagementservice.PaymentRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class  PaymentRecordsServiceImpl extends ServiceImpl<PaymentRecordsMapper, PaymentRecords> implements PaymentRecordsService {
    @Autowired
    private PaymentRecordsMapper paymentRecordsMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insert(PaymentRecords paymentRecords) {
        return paymentRecordsMapper.insert(paymentRecords);
    }


    @Override
    public PaymentRecords findById(Long id) {
        return paymentRecordsMapper.findById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(PaymentRecords paymentRecords) {
        if (paymentRecords.getId() == null) {
            return 0; // 如果ID为空，则更新失败
        }
        return paymentRecordsMapper.update(paymentRecords) > 0 ? 1 : 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(Long id) {
        return paymentRecordsMapper.deleteById(id);
    }

    @Override
    public List<PaymentRecords> selectPage(Map<String, Object> params) {
        int pageNum = (Integer) params.get("pageNum");
        int pageSize = (Integer) params.get("pageSize");
        params.put("offset", (pageNum - 1) * pageSize);
        params.put("limit", pageSize);
        return paymentRecordsMapper.selectPage(params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer selectTotal(Map<String, Object> params) {
        return paymentRecordsMapper.selectTotal(params);
    }

    /**
     *
     * @deprecated 获取该账号所有未认领的收款记录
     */
    @Override
    public PaymentRecordsVos getAllPayMentRecords(IPage<PaymentRecords> page, PaymentRecordsDtos paymentRecordsDtos) {
        List<PaymentRecords> paymentRecordsList = null;
        long total = 0L;
        IPage<PaymentRecords> iPage = baseMapper.getAllPayMentRecords(page, paymentRecordsDtos);
        paymentRecordsList = iPage.getRecords();
        total = iPage.getTotal();

        return new PaymentRecordsVos(paymentRecordsList,total);
    }
    /**
     *
     * @deprecated 将该客户添加到收款记录对应的收款记录的付款业主中
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addPaymentRecordsCustomerId(PaymentRecordsAddDto paymentRecordsAddDto,Integer uid) {
        Integer[] arr = paymentRecordsAddDto.getPayIdList();
        List<PaymentRecords> paymentRecordsList = new ArrayList<>();
        int count = 0;
        for (Integer l : arr) {
            PaymentRecords paymentRecords = new PaymentRecords();
            paymentRecords.setCustomerid(paymentRecordsAddDto.getCustomerid());
            paymentRecords.setIsclaim(1L);
            paymentRecords.setApplication(uid);
            paymentRecords.setId(l);
            paymentRecordsList.add(paymentRecords);
        }
        for (PaymentRecords paymentRecords : paymentRecordsList) {
            baseMapper.update(paymentRecords);
            count++;
        }
        return count > 0 && count == arr.length;
    }
    /**
     *
     * @deprecated 将该客户从收款记录对应的收款记录中移除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean revokePaymentRecordsClaim(PaymentRecordsRevokeDto paymentRecordsRevokeDto) {
        Integer[] arr = paymentRecordsRevokeDto.getPayIdList();
        List<PaymentRecords> paymentRecordsList = new ArrayList<>();
        int count = 0;
        for (Integer l : arr) {
            PaymentRecords paymentRecords = new PaymentRecords();
            paymentRecords.setId(l);
            paymentRecordsList.add(paymentRecords);
        }
        for (PaymentRecords paymentRecords : paymentRecordsList) {
            baseMapper.revokePaymentRecordsClaim(paymentRecords.getId());
            count++;
        }
        return count > 0 && count == arr.length;
    }
    /**
     *
     * @deprecated 获取该客户认领的收款记录
     */
    @Override
    public PaymentRecordsVos getAllClaimPayMentRecords(IPage<PaymentRecords> page, PaymentRecordsDtos paymentRecordsDtos,String account) {
        List<PaymentRecords> paymentRecordsList = null;
        long total = 0L;
        IPage<PaymentRecords> iPage = baseMapper.getAllClaimPayMentRecords(page, paymentRecordsDtos,account);
        paymentRecordsList = iPage.getRecords();
        total = iPage.getTotal();
        return new PaymentRecordsVos(paymentRecordsList,total);
    }

    @Override
    public int updateRefundamount(Long id) {
        return paymentRecordsMapper.updateRefundamount(id);
    }
    /**
     *
     * @deprecated 获取该客户所有收款记录
     */
    @Override
    public AllPaymentRecordsVo getAllPaymenRecordsByCustomerId(IPage<PaymentRecords> page, PaymentRecordsCustomerDto paymentRecordsCustomerDto,String account) {
        List<PaymentRecordsInfo> list = null;
        long total = 0L;
        IPage<PaymentRecordsInfo> iPage = baseMapper.getAllPaymenRecordsByCustomerId(page, paymentRecordsCustomerDto,account);
        list = iPage.getRecords();
        total = iPage.getTotal();
        return new AllPaymentRecordsVo(list,total);
    }

    @Override
    public List<PaymentDTO> findBy(Long billid) {
        return paymentRecordsMapper.findBy(billid);
    }

    @Override
    public PaymentRecords findByCollectionNumber(String collectionnumber) {
        return paymentRecordsMapper.findByCollectionNumber(collectionnumber);
    }


    @Override
    public PaymentRecords findByBill(int billid) {
        return paymentRecordsMapper.findByBill(billid);
    }


}
